package com.qining618.cms.cms.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qining618.cms.cms.domain.Content;
import com.qining618.cms.cms.enums.ContentOperateEnum;
import com.qining618.cms.cms.enums.ContentStatusEnum;
import com.qining618.cms.cms.mapper.ContentMapper;
import com.qining618.cms.cms.service.ChannelContentService;
import com.qining618.cms.cms.service.ContentRecordService;
import com.qining618.cms.cms.service.ContentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qining618.common.exception.BusinessException;
import com.qining618.common.result.ErrorCode;
import com.qining618.common.utils.Query;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * CMS内容 服务实现类
 * </p>
 *
 * @author qining
 * @date 2019-09-17
 */
@Transactional(readOnly = true, rollbackFor = Exception.class)
@Service
public class ContentServiceImpl extends ServiceImpl<ContentMapper, Content> implements ContentService {

    @Autowired
    private ChannelContentService channelContentService;
    @Autowired
    private ContentRecordService contentRecordService;

    @Override
    public IPage<Content> page(Map<String, Object> params) {
        return this.baseMapper.page(Query.of(params), params);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Content save(Content content, Long userId, Boolean draft, Boolean contribute, List<Long> channelIds) {
        // 更新检验是否存在
        if (content.getId() != null) {
            boolean isExist = this.baseMapper.existById(content.getId());
            if (!isExist) {
                throw new BusinessException(ErrorCode.DATA_NOT_FOUND);
            }
        }

        if (BooleanUtils.isTrue(contribute)) {
            // 投稿
            content.setStatus(ContentStatusEnum.CONTRIBUTE);
        } else if (BooleanUtils.isTrue(draft)) {
            // 草稿
            content.setStatus(ContentStatusEnum.DRAFT);
        } else {
            // 发布
            content.setStatus(ContentStatusEnum.CHECKED);
            if (content.getReleaseDate() == null) {
                content.setReleaseDate(new Date());
            }
        }

        if (content.getId() != null) {
            // 更新
            int opNum = this.baseMapper.updateById(content);
            if (opNum != 1) {
                throw new BusinessException(ErrorCode.DB_ERROR);
            }
            // 保存操作记录
            this.contentRecordService.save(content.getId(), userId, ContentOperateEnum.EDIT);
            // 删除原有栏目内容关联信息
            this.channelContentService.deleteByContentId(content.getId());
        } else {
            // 新增
            int opNum = this.baseMapper.insert(content);
            if (opNum != 1) {
                throw new BusinessException(ErrorCode.DB_ERROR);
            }
            // 保存操作记录
            this.contentRecordService.save(content.getId(), userId, ContentOperateEnum.ADD);
        }

        // 保存栏目内容关联信息
        if (channelIds != null && channelIds.size() > 0) {
            for (Long channelId : channelIds) {
                this.channelContentService.save(channelId, content.getId());
            }
        }

        return content;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void setTopLevel(Long id, Integer level, Date dueTime) {
        boolean isExist = this.baseMapper.existById(id);
        if (!isExist) {
            throw new BusinessException(ErrorCode.DATA_NOT_FOUND, "内容不存在");
        }
        Content upContent = new Content();
        upContent.setId(id).setTopLevel(level).setTopLevelDueTime(dueTime);
        int opNum = this.baseMapper.updateById(upContent);
        if (opNum != 1) {
            throw new BusinessException(ErrorCode.DB_ERROR);
        }
    }
}
